﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FRW.Common;
using FRW.Common.Configuration;
using FRW.Common.Extensions;

namespace Money.Configuration
{
    // TODO have a framework for configuration that can lookup a value based on a config item name and return the object value, this may give a level of additional abstraction?
    [Serializable]
    public class ApplicationConfiguration : IConfiguration
    {
        public Categories Categories;
        public List<DescriptionMatch> DescriptionMatches = new List<DescriptionMatch>();
        public List<string> FileNamePatterns = new List<string>();
        public List<BackupItem> BackupItems = new List<BackupItem>(10);
        public string ImportPath { get; set; }
        public string ImportSavePath { get; set; }
        public bool MoveFileOnImport { get; set; }
        public decimal HighlightAbove { get; set; }
        public bool AutoSave { get; set; }
        public bool AutoSaveData { get; set; }
        public bool AutoSaveConfiguration { get; set; }
        public bool AutoReconcileTransfersOnImport { get; set; }
        public int BackupInterval { get; set; }

        public string ApplicationName
        {
            get
            {
                return Application.ProductName;
            }
        }

        public void DefaultConfiguration()
        {
            AutoSave = true;
            
            MoveFileOnImport = false;

            CheckConfiguration();
        }

        public void CheckConfiguration()
        {
            CheckCategories();

            if (FileNamePatterns == null)
            {
                FileNamePatterns = new List<string>();
            }

            if (FileNamePatterns.Count == 0)
            {
                FileNamePatterns.Add(@"tesco-....-..\.tsv");
                FileNamePatterns.Add(@"current-........\.csv");
                FileNamePatterns.Add(@"report-......\.csv");
            }

            if (String.IsNullOrWhiteSpace(ImportPath))
            {
                ImportPath = PathManager.Instance.MyDocuments;
            }

            if (String.IsNullOrWhiteSpace(ImportSavePath))
            {
                ImportSavePath = PathManager.Instance.MyDocuments + "\\imported";
            }

            if (HighlightAbove < 10)
            {
                HighlightAbove = 500;
            }
        }

        private void CheckCategories()
        {
            if (Categories == null)
            {
                Categories = new Categories();
            }

            // check that we have the default categories available
            foreach (Categories.DefaultCategories category in Enum.GetValues(typeof(Categories.DefaultCategories)))
            {
                int enumValue = (int)category;
                Category categoryItem = Categories.Find(c => c.Id == enumValue);

                if (categoryItem == null)
                {
                    Categories.Add(new Category() { Id = enumValue, Name = category.ToString(), ParentId = 0 });
                }
                else
                {
                    // TODO maybe want to check if this has changed and if so set it back to the default values
                }
            }
        }
    }
}
